﻿<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>文档管理</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
  <link rel="stylesheet" href="/plugins/layui/css/layui.css" media="all">
  <link rel="stylesheet" href="/plugins/layuiadmin/style/admin.css" media="all">
  <style type="text/css">
   .layui-select-title input{
       width:200px;
   }
  </style>
</head>
<body>
  <div class="layui-fluid">
    <div class="layui-card">
      <div class="layui-card-body">
        <div style="padding-bottom: 10px;">
          <button class="layui-btn layui-btn-danger layuiadmin-btn-list" data-type="del"><i class="layui-icon layui-icon-delete"></i>删除</button>
          <button class="layui-btn layui-btn-normal layuiadmin-btn-list" data-type="add"><i class="layui-icon layui-icon-add-1"></i>添加</button>
          <button class="layui-btn layuiadmin-btn-list" data-type="refresh"><i class="layui-icon layui-icon-refresh"></i>刷新</button>
          <div class="layui-form" lay-filter="dds-search-form" id="dds-search-form" style="float:right">
            <div class="layui-inline">
            <select name="doctype" id="doctype">
                <option value="">选择文档类别</option>
            </select>
            </div>
            <div class="layui-inline">
            <select name="orgtype" id="orgtype">
                <option value="">选择发布机关</option>
            </select>
            </div>
            <div class="layui-input-inline">
            <select name="yeartype" id="yeartype">
                <option value="">选择发布年份</option>
            </select>
            </div>
            <div class="layui-inline">
             <input class="layui-input" name="keyword" id="keyword" placeholder="请输入搜索条件">
            </div>
            <button class="layui-btn" lay-submit lay-filter="btn_search"><i class="layui-icon layui-icon-search"></i>搜索</button>
          </div>
        </div>
        <table id="dds-list" lay-filter="dds-list"></table>
        <script type="text/html" id="table-content-list">
          <a class="layui-btn layui-btn-xs layui-btn-normal" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
          <a class="layui-btn layui-btn-xs layui-btn-container" lay-event="view"><i class="layui-icon layui-icon-edit"></i>查看</a>
          <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="download"><i class="layui-icon layui-icon-down"></i>下载</a>
        </script>
      </div>
    </div>
  </div>
  <script src="/plugins/layui/layui.js"></script> 
  <script>
  layui.config({
    base: '/plugins/layuiadmin/' //静态资源所在路径
  }).extend({
    index: 'lib/index' //主入口模块
  }).use(['index','table','laydate','form'], function(){
    var $ = layui.$, table = layui.table, form = layui.form, laydate = layui.laydate;

    $.ajax({
        type: "get",
        url: "/type/get_typelist",
        data: {markup:'doctype'},
        dataType: "json",
        async:false,
        success: function (res) {
            $.each(res.typelist, function (i, v) {
                $("#doctype").append("<option value='" + v.id + "'>" + v.title + "</option>");
            });
            layui.form.render("select");
        }
    });

    $.ajax({
        type: "get",
        url: "/type/get_typelist",
        data: {markup:'orgtype'},
        dataType: "json",
        async:false,
        success: function (res) {
            $.each(res.typelist, function (i, v) {
                $("#orgtype").append("<option value='" + v.id + "'>" + v.title + "</option>");
            });
            layui.form.render("select");
        }
    });

    $.ajax({
        type: "get",
        url: "/type/get_typelist",
        data: {markup:'yeartype'},
        dataType: "json",
        async:false,
        success: function (res) {
            $.each(res.typelist, function (i, v) {
                $("#yeartype").append("<option value='" + v.id + "'>" + v.title + "</option>");
            });
            layui.form.render("select");
        }
    });

    table.render({
      elem: '#dds-list'
      , url: 'doc_list' //接口
        , cols: [[
          { type: 'checkbox', fixed: 'left' }
        , { field: 'number', title: '文号', width: 150, align: 'center' }
        , { field: 'title', title: '文档名称', minWidth: 200, align: 'center' }
        , { field: 'doctype_name', title: '文档类别',width:100 , align: 'center' }
        , { field: 'orgtype_name', title: '发布机关', width:150 , align: 'center' }
        , { field: 'yeartype_name', title: '发布年份', width: 90 , align: 'center'}
        , { field: 'datetime', title: '添加时间', width: 160, align: 'center' }
        , { title: '操作', width: 230, align: 'center', fixed: 'right', toolbar: '#table-content-list' }
      ]]
      , page: true
      , request: {
        pageName: 'pageindex', //页码的参数名称，默认：page
        limitName: 'pagesize' //每页数据量的参数名，默认：limit
      }
      , limit: 20
      , limits: [20, 50, 100]
      , text: {
        none: '暂无相关数据'
      }
    });

    //监听搜索
    form.on('submit(btn_search)', function(data){
      var field = data.field;
      //执行重载
      table.reload('dds-list', {
        where: field
      });
    });

    //监听操作
    var active = {
      del: function(){
        var checkStatus = table.checkStatus('dds-list')
        ,checkData = checkStatus.data; //得到选中的数据
        if(checkData.length === 0){
          return layer.msg('请选择数据');
        }

        var ids = [];//存储id的数组
        checkData.forEach(function (item) {
          ids.push(item.id);
        });
      
        layer.confirm('您确定要删除吗？删除后将无法进行恢复。',{icon: 3, title:'提示'}, function (index) {
          $.ajax({
                type: "post",
                url: "doc_list",
                data: {ids:ids.join(","),action:'delete'},
                dataType: "json",
                success: function (data) {
                    if (data.code == 0)
                    {
                      table.reload('dds-list');
                      layer.msg(data.msg);
                    }
                }
          });
        });
      },
      add: function(){
        layer.open({
          type: 2
          ,title: '添加文档'
          ,content: 'doc_add.html'
          ,maxmin: true
          ,area: ['700px', '600px']
          ,btn: ['确定', '取消']
          ,yes: function(index, layero){
            //点击确认触发 iframe 内容中的按钮提交
            var submit = layero.find('iframe').contents().find("#btn_submit");
            submit.click();
          }
        }); 
      },
      refresh: function () {
        table.reload('dds-list');
      }
    }; 

    $('.layui-btn.layuiadmin-btn-list').on('click', function(){
      var type = $(this).data('type');
      active[type] ? active[type].call(this) : '';
    });

    //监听工具条
    table.on('tool(dds-list)', function(obj){
      var data = obj.data;
        if(obj.event === 'edit'){
            layer.open({
              type: 2
              ,title: '编辑文档'
              ,content: 'doc_edit.html#/id='+data.id
              ,maxmin: true
              ,area: ['700px', '600px']
              ,btn: ['确定', '取消']
              ,yes: function(index, layero){
                var submit = layero.find('iframe').contents().find("#btn_submit");
                submit.trigger('click');
                }
            });
        } else if (obj.event === 'view') {
            if (data.filetype == '.doc' || data.filetype == '.docx' || data.filetype == '.pdf') {
                window.open('/fppdf/index.html?docpath=' + data.docfile);
            }
            else {
                window.open(data.docfile);
            }
        }  else if(obj.event === 'download'){
            window.open(data.docfile);
        } 
    });

  });
  </script>
</body>
</html>
